package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author np
 * @Date 2020/10/9 10:06
 * @Version 1.0
 * @Description: 角色
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;


    //新增
//    @RequestMapping("/add")
//    public Result add(@RequestBody Role role, Integer[] permissionIds){
//        try{
//            roleService.add(role,permissionIds);
//            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false, MessageConstant.ADD_USER_FAIL);
//        }
//    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list = roleService.findAll();//发送请求
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer userId){
        try{
            List<Integer> list = roleService.findRoleIdsByUserId(userId);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

}
